package io.scnb.design.factory;

import io.scnb.design.workshop.ICacheAdapter;

import java.lang.reflect.Proxy;

public class JDKProxyFactory {

    public static <T> T getProxy(Class<T> cacheClazz, Class<? extends ICacheAdapter> cacheAdapter) throws InstantiationException, IllegalAccessException {
        // 实际是去调用传入的cacheAdapter实例的方法来完成缓存的相关操作
        JDKInvocationHandler handler = new JDKInvocationHandler(cacheAdapter.newInstance());
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        return (T) Proxy.newProxyInstance(contextClassLoader, new Class[]{cacheClazz}, handler);
    }
}
